package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import server.UserServer;
import serverImp.UserServerImp;
import entity.User;

public class RegServlet extends HttpServlet {

	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		String name= request.getParameter("id");
		String pwd=request.getParameter("pwd");
		int auth=Integer.parseInt(request.getParameter("authority"));
		User user=new User(name, pwd, auth);
		UserServer ser=new UserServerImp();
		if(ser.add(user)>0){
			out.print("<script type='text/javascript'>alert('注册成功');location.href='login.jsp';</script>");
		}else{
			request.getSession().setAttribute("error", "用户名已存在");
			response.sendRedirect("regist.jsp");
			return;
		}
	}

}
